package com.zkr.kfkw.service;

import com.zkr.kfkw.mapper.KFKWMapper;
import com.zkr.kfkw.model.*;
import com.zkr.kqsx.model.KQSXVo;
import com.zkr.subclass.enummodel.StatusEnum;
import com.zkr.util.AllRecords;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * Created by lenovo on 2021/1/18.
 */
@Service
public class KFKWServiceImpl implements KFKWService {

    @Autowired
    private KFKWMapper kfkwMapper;

    @Override
    public ResultVo queryPage(KFKWSelectVo kfkwSelectVo) {
        int count = kfkwMapper.queryCount(kfkwSelectVo);
        List<KFKWVo> kfkwVoList = kfkwMapper.queryPage(kfkwSelectVo);
        for (KFKWVo kfkwVo:kfkwVoList){
            Integer status = kfkwVo.getStatus() == true?1:0;
            for (StatusEnum c:StatusEnum.values()){
                if (c.getValue().equals(status)){
                    kfkwVo.setStatusName(c.getName());
                }
            }
        }
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(kfkwSelectVo.getPageIndex());
        allRecords.setDataList(kfkwVoList);
        allRecords.setPageSize(kfkwSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);
    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid KFKWUpdateStatusVo kfkwUpdateStatusVo) {
        KFKWVo kfkwVo = new KFKWVo();
        BeanUtils.copyProperties(kfkwUpdateStatusVo,kfkwVo);
        kfkwVo.setUpdateName("李四");
        kfkwVo.setUpdateTime(new Date());
        kfkwMapper.updateStatus(kfkwVo);
        return ResultBuilderVo.success();

    }

    @Override
    public ResultVo query(Integer id) {
      // KQVo kqVo = kfkwMapper.query(wareHouseId);
       List<KQSelectVo> kqSelectVoList = kfkwMapper.query(id);
       return ResultBuilderVo.success(kqSelectVoList);


    }

    @Override
    public ResultVo querykw(KWVo kwVo) {
        //KWVo kwVo = kfkwMapper.querykw(kqcode);
        List<KWVo>kwVoList = kfkwMapper.querykw(kwVo);
        return ResultBuilderVo.success(kwVoList);

    }

    @Override
    @Transactional
    public ResultVo add(@Valid List<KFKWAddVo> kfkwAddVoList) {
        String code =null;
        String name =null;
        for (KFKWAddVo kfkwAddVo : kfkwAddVoList) {
            code= kfkwAddVo.getCode();
            name= kfkwAddVo.getName();
        }
        KFKWVo kFKWName = kfkwMapper.queryByName(name);
        if (kFKWName!=null){
            return ResultBuilderVo.error("同一库区下库位名称已经存在了");
        }
        KFKWVo KFKWCode =  kfkwMapper.queryByCode(code);
        if (KFKWCode!=null){
            return ResultBuilderVo.error("库位编码已经存在了");
        }
        HashSet<KFKWAddVo> kfkwAddVos = new HashSet<>(kfkwAddVoList);
        if(kfkwAddVos.size() != kfkwAddVoList.size()){
            return ResultBuilderVo.error("数据重复");
        }
        Date date = new Date();
        for(KFKWAddVo list:kfkwAddVoList){
            list.setCreateName("李四");
            list.setCreateTime(date);
            list.setUpdateName("李四");
            list.setUpdateTime(date);
        }
        kfkwMapper.delete(kfkwAddVoList);
        kfkwMapper.add(kfkwAddVoList);

        return ResultBuilderVo.success();


    }

    @Override
    @Transactional
    public ResultVo update(@Valid KFKWUpdateVo kfkwUpdateVo) {
        KFKWVo kFKWName = kfkwMapper.queryByName(kfkwUpdateVo.getName());
        if (kFKWName != null){
            if (!(kFKWName.getId().equals(kFKWName.getId()))){
                return ResultBuilderVo.error("库位名称已经存在");
            }
        }
        KFKWVo kfkwVo = new KFKWVo();
        BeanUtils.copyProperties(kfkwUpdateVo,kfkwVo);
        kfkwVo.setUpdateName("李四");
        kfkwVo.setUpdateTime(new Date());
        kfkwMapper.update(kfkwVo);
        return ResultBuilderVo.success();

    }




    /*@Override
    @Transactional
    public ResultVo update(@Valid KFKWUpdateVo kfkwUpdateVo) {
        KFKQVo kqName = kfkqMapper.queryName(kfkqUpdateVo.getName());
        if (kqName != null) {
            if (!(kqName.getId().equals(kqName.getId()))) {
                return ResultBuilderVo.error("库区名称不能重复!");
            }
        }
        KFKQVo kqCode = kfkqMapper.queryCode(kfkqUpdateVo.getCode());
        if (kqCode != null){
            if (!(kqCode.getId().equals(kqCode.getId()))){
                return ResultBuilderVo.error("库区编码不能重复!");
            }
        }
        KFKQVo kfkqVo = new KFKQVo();
        BeanUtils.copyProperties(kfkqUpdateVo,kfkqVo);
        kfkqVo.setUpdateName("李四");
        kfkqVo.setUpdateTime(new Date());
        kfkqMapper.update(kfkqVo);
        return ResultBuilderVo.success();

    }*/

}
